#include "function.h"
#include <QDebug>

Attraction::Attraction(QString name, QString description, int daily_limit, int today, double full, double half) {
    this->name = name;
    this->description = description;
    this->daily_limit = daily_limit;
    this->booked_today = 0;
    // 初始化票种
    ticket_types["free"] = Ticket("Free", 0.0);
    ticket_types["half"] = Ticket("Half Price", half);
    ticket_types["full"] = Ticket("Full Price", full);
    booked_today = today;
}

void Attraction::show_info() {
    qDebug() << "景点名称: " << name << endl;
    qDebug() << "景点介绍: " << description << endl;
    qDebug() << "可预约的票种及价格:" << endl;

    QMap<QString, Ticket>::const_iterator it = ticket_types.constBegin();
    while(it != ticket_types.constEnd())
    {
        qDebug() << it.key() << ":" << it.value().ticket_type << ":" << it.value().price;
    }

    qDebug() << "每日预约限额: " << daily_limit << "人，已预约: " << booked_today
         << "人" << endl;
}

bool Attraction::can_book() {
    return booked_today < daily_limit;
}

void Attraction::book_ticket() {
    if (can_book()) {
        booked_today++;
        qDebug() << "预约成功！" << endl;
    } else {
        qDebug() << "今日预约已满，无法预约！" << endl;
    }
}

void Attraction::cancel_ticket() {
    if (booked_today > 0) {
        booked_today--;
        qDebug() << "预约取消成功！" << endl;
    } else {
        qDebug() << "当前没有预约可以取消！" << endl;
    }
}

Visitor::Visitor(QString username, QString name, QString contact, QString id_card,
                 QString id_type, bool is_admin)
{
    this->username = username;
    this->name = name;
    this->contact = contact;
    this->id_card = id_card;
    this->id_type = id_type;
    this->is_admin = is_admin;
}

System::System() {
    // 初始化景点信息
    //attractions << Attraction("岳麓山", "岳麓山是长沙的著名景点之一，风景如画。", 100);
    //attractions << Attraction("橘子洲", "橘子洲是长沙的文化象征，坐落在湘江中央。", 50);
}

// 游客注册
void System::register_visitor(QString username, QString name, QString contact, QString id_card,
                              QString id_type, bool is_admin)
{

    visitors << Visitor(username, name, contact, id_card, id_type, is_admin);
    //qDebug() << "注册成功！";
    //qDebug() << visitors.size();
}
// 游客登录
Visitor *System::login(QString username) {
    for(int i=0; i<visitors.length(); i++)
    {
        qDebug() << visitors.at(0).username;
        if(visitors.at(i).username == username)
        {
            logged_in_users[username] = (Visitor *)(&(visitors.at(i)));
            return (Visitor *)(&(visitors.at(i)));
        }
    }
    return nullptr;
}

// 预约景点票
void System::book_ticket(Visitor *visitor) {
    if (visitor == nullptr) {
        qDebug() << "请先登录！" << endl;
        return;
    }

    qDebug() << "请选择预约景点:" << endl;
    for (int i = 0; i < attractions.size(); i++) {
        qDebug() << i + 1 << ". " << attractions[i].name << endl;
    }

    int choice;
    qDebug() << "请输入景点编号: ";
    std::cin >> choice;

    if (choice < 1 || choice > attractions.size()) {
        qDebug() << "无效选择!" << endl;
        return;
    }

    Attraction &attraction = attractions[choice - 1];

    // 根据身份分类设置票价
    QString ticket_type;
    if (visitor->id_type == "身份证") {
        ticket_type = "full";
    } else if (visitor->id_type == "学生证") {
        ticket_type = "half";
    } else {
        ticket_type = "free";
    }

    qDebug() << "您选择的票种为: " << attraction.ticket_types[ticket_type].ticket_type
         << endl;
    qDebug() << "票价: " << attraction.ticket_types[ticket_type].price << "元"
         << endl;

    // 预约操作
    attraction.book_ticket();
}

// 取消预约
void System::cancel_ticket(Visitor *visitor) {
    if (visitor == nullptr) {
        qDebug() << "请先登录！" << endl;
        return;
    }

    qDebug() << "请选择取消预约的景点:" << endl;
    for (int i = 0; i < attractions.size(); i++) {
        qDebug() << i + 1 << ". " << attractions[i].name << endl;
    }

    int choice;
    qDebug() << "请输入景点编号: ";
    std::cin >> choice;

    if (choice < 1 || choice > attractions.size()) {
        qDebug() << "无效选择!" << endl;
        return;
    }

    Attraction &attraction = attractions[choice - 1];
    attraction.cancel_ticket();
}

// 显示景点信息
void System::show_attractions() {
    for (auto &attraction : attractions) {
        attraction.show_info();
    }
}

// 管理员统计信息
void System::admin_statistics() {
    qDebug() << "每日预约统计:" << endl;
    for (auto &attraction : attractions) {
        qDebug() << "景点: " << attraction.name
             << ", 今日已预约人数: " << attraction.booked_today << "人" << endl;
    }
}
